<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/12
 * Time: 10:47
 */
namespace app\libs;
use think\Config;
class SendSms
{
    // 只匹配数字
    function strint($str){
        if($str){
            preg_match("/\d+/is",$str,$v);
        }
        $StrInt = @$v[0];
        if($StrInt){
            return $StrInt;
        }else{
            return $str;
        }
    }
    public function send_sms($data = [])
    {
        $url = "http://v.juhe.cn/sms/send";
        $params = array(
            'key'   => Config::get('mobile_code.appkey'), //您申请的APPKEY
            'mobile'    => $data['phone'], //接受短信的用户手机号码
            'tpl_id'    => $data['tpl_id'], //您申请的短信模板ID，根据实际情况修改
            'tpl_value' => $this->value($data['value']), //1234#code#=1234&#company#=聚合数据' //您设置的模板变量，根据实际情况修改
        );

        $paramstring = http_build_query($params);
        $content = self::juheCurl($url, $paramstring);
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //状态为0，说明短信发送成功
                // echo "短信发送成功,短信ID：".$result['result']['sid'];
                return $data = array(
                    "msg"   => true,
                    "code"  => $this->strint($params['tpl_value']),
                    "res"   => '短信发送成功'
                );
            }else{
                //状态非0，说明失败
                $msg = $result['reason'];
                // echo "短信发送失败(".$error_code.")：".$msg;
                return $data = array(
                    "msg"   => false,
                    "res"   => '短信发送失败'
                );
            }
        }else{
            //返回内容异常，以下可根据业务逻辑自行修改
            // echo "请求发送短信失败";
            return $data = array(
                "msg"   => false,
                "res"   => '短信发送失败'
            );
        }
    }

    /**
     * 随机拼接六位验证码
     * @return int 验证码
     */
    public function rand(){
        return rand(1000, 9999);
    }

    /**
     * 获取验证码的值
     * @return string 验证码的值
     */
    public function value($code) {
        $rand = $code;
        if ($code === 1){
            $rand = $this->rand();
        }
        return $this->value = '#code#='.$rand;
    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    public static function juheCurl($url, $params = false, $ispost = 0)
    {
    $httpInfo = array();
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'JuheData');
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    if ($ispost) {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_URL, $url);
    } else {
        if ($params) {
            curl_setopt($ch, CURLOPT_URL, $url.'?'.$params);
        } else {
            curl_setopt($ch, CURLOPT_URL, $url);
        }
    }
    $response = curl_exec($ch);
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return false;
    }
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
    curl_close($ch);
    return $response;
}
}